/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dp1.pucp.Compras.DAO;

import com.dp1.pucp.Modelo.Clases.Costoinsumos;
import com.dp1.pucp.Modelo.Clases.Departamento;
import com.dp1.pucp.Modelo.Clases.Distrito;
import com.dp1.pucp.Modelo.Clases.Proveedor;
import com.dp1.pucp.Modelo.Clases.Provincia;
import com.dp1.pucp.Modelo.Clases.Tipoproducto;
import com.dp1.pucp.Modelo.Clases.Moneda;
import com.dp1.pucp.Modelo.Clases.Costoinsumos;
import com.dp1.pucp.Util.HibernateUtil;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
 *
 * @author ubuntu64
 */
public class ProveedorDAO {
    
    public void guardarProveedor(Proveedor proveedor){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx =  session.beginTransaction();
        try {
            session.saveOrUpdate(proveedor);
            tx.commit();
        }catch(HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();            
        }        
    }
    
    public ArrayList<Costoinsumos> listarCostos(Proveedor proveedor){
         ArrayList<Costoinsumos> costos = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Costoinsumos where idproveedor= "+ proveedor.getIdproveedor()+" order by idtipoproducto").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Costoinsumos d = (Costoinsumos) iter.next();
                Hibernate.initialize(d.getIdcostoinsumos());
                Hibernate.initialize(d.getCosto());
                Hibernate.initialize(d.getFechainicio());
                Hibernate.initialize(d.getFechafin());
                Hibernate.initialize(d.getMoneda());
                Hibernate.initialize(d.getProveedor());
                Hibernate.initialize(d.getProveedor().getIdproveedor());
                Hibernate.initialize(d.getProveedor().getRazonsocial());
                Hibernate.initialize(d.getTipoproducto());                
                Hibernate.initialize(d.getTipoproducto().getIdtipoproducto());                
                Hibernate.initialize(d.getTipoproducto().getNombre());                
                costos.add(d);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return costos;
        }  
    }
            
     public void guardarCosto(Costoinsumos costo){
       Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx =  session.beginTransaction();
        try {
            session.saveOrUpdate(costo);
            tx.commit();
        }catch(HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();            
        }        
    }
     
        
    public void guardarCostoInsumo(Costoinsumos costoinsumo){
        Costoinsumos c = new Costoinsumos();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx =  session.beginTransaction();
        try {
            session.saveOrUpdate(costoinsumo);
            session.flush();
         
        }catch(HibernateException e) {
            throw new HibernateException(e);
        } finally {
            
            session.persist(costoinsumo);
            
            int cod = costoinsumo.getIdcostoinsumos();          
            List lista = session.createQuery("from Costoinsumos where idcostoinsumos="+cod).list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) { 
                c = (Costoinsumos) iter.next();
            }        
            tx.commit();
            session.close();
            
        }
        
    }
     
    public ArrayList<String> listarEstado() 
    {
        ArrayList<String> estados = new ArrayList<>();
        estados.add("Seleccionar");
        estados.add("Inactivo");
        estados.add("Activo");
        return estados;
    }
    
    public ArrayList<String> listarCategoria() 
    {
        ArrayList<String> estados = new ArrayList<>();
        estados.add("Alto");
        estados.add("Medio");
        estados.add("Bajo");
        return estados;
    }
    
    
    public Costoinsumos sacarCostoInsumos(int codTipoProd){
        ArrayList<Costoinsumos> costoInsumos = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Costoinsumos where idtipoproducto= "+ codTipoProd +" order by idtipoproducto").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Costoinsumos d = (Costoinsumos) iter.next();
                Hibernate.initialize(d.getTipoproducto());
                Hibernate.initialize(d.getCosto());
                Hibernate.initialize(d.getFechainicio());
                Hibernate.initialize(d.getFechafin());
                Hibernate.initialize(d.getIdcostoinsumos());
                Hibernate.initialize(d.getProveedor());
                costoInsumos.add(d);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return costoInsumos.get(0);
        }     
    }
            
            
    public ArrayList<Distrito> listarDistrito(Provincia p){
        ArrayList<Distrito> distritos = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Distrito where idprovincia= "+ p.getIdprovincia()+" order by iddistrito").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Distrito d = (Distrito) iter.next();
                Hibernate.initialize(d.getIddistrito());
                Hibernate.initialize(d.getProvincia());                
                distritos.add(d);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return distritos;
        }     
    }
    
    public ArrayList<Costoinsumos> listarProductos(int codProv){
        ArrayList<Costoinsumos> tipoProductos = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Costoinsumos where idproveedor= "+ codProv +" order by idproveedor").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Costoinsumos d = (Costoinsumos) iter.next();
                Hibernate.initialize(d.getIdcostoinsumos());
                Hibernate.initialize(d.getFechainicio());
                Hibernate.initialize(d.getFechafin());
                Hibernate.initialize(d.getTipoproducto().getDescripcion());
                tipoProductos.add(d);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return tipoProductos;
        }     
    }
    
     public ArrayList<Provincia> listarProvincia(Departamento d){
        ArrayList<Provincia> provincias = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Provincia where iddepartamento = "+d.getIddepartamento()+" order by idprovincia").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Provincia p = (Provincia) iter.next();
                
                Hibernate.initialize(p.getIdprovincia());
                Hibernate.initialize(p.getDepartamento());                
                provincias.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return provincias;
        }     
    }
      
       public ArrayList<Departamento> listarDepartamento(){
        ArrayList<Departamento> departamentos = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Departamento order by iddepartamento").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Departamento d = (Departamento) iter.next();
                
                Hibernate.initialize(d.getIddepartamento());
                                
                departamentos.add(d);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return departamentos;
        }     
    }
       
     /* para la lista de proveedores */
       
       public ArrayList<Proveedor> listarProveedor(int estadoProv)
    {
        ArrayList<Proveedor> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Proveedor where estado = "+ estadoProv +" order by idproveedor").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Proveedor p = (Proveedor) iter.next();
                Hibernate.initialize(p.getRazonsocial());
                Hibernate.initialize(p.getRuc());
                Hibernate.initialize(p.getContacto());                
                Hibernate.initialize(p.getCorreo());
                Hibernate.initialize(p.getTelefonocontacto());                
                Hibernate.initialize(p.getDireccionfiscal()); 
                Hibernate.initialize(p.getEstado());                
                Hibernate.initialize(p.getDistrito());
                Hibernate.initialize(p.getDistrito().getIddistrito());
                Hibernate.initialize(p.getDistrito().getProvincia());
                Hibernate.initialize(p.getDistrito().getProvincia().getIdprovincia());
                Hibernate.initialize(p.getDistrito().getProvincia().getDepartamento());
                Hibernate.initialize(p.getDistrito().getProvincia().getDepartamento().getIddepartamento());
                Hibernate.initialize(p.getIdproveedor());                
                listaResultados.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }    
       
         public ArrayList<Proveedor> listarProveedorSolo()
    {
        ArrayList<Proveedor> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Proveedor order by idproveedor").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Proveedor p = (Proveedor) iter.next();
                Hibernate.initialize(p.getRazonsocial());
                Hibernate.initialize(p.getRuc());
                Hibernate.initialize(p.getContacto());                
                Hibernate.initialize(p.getCorreo());
                Hibernate.initialize(p.getTelefonocontacto());                
                Hibernate.initialize(p.getDireccionfiscal()); 
                Hibernate.initialize(p.getEstado());                
                Hibernate.initialize(p.getDistrito());
                Hibernate.initialize(p.getDistrito().getIddistrito());
                Hibernate.initialize(p.getDistrito().getProvincia());
                Hibernate.initialize(p.getDistrito().getProvincia().getIdprovincia());
                Hibernate.initialize(p.getDistrito().getProvincia().getDepartamento());
                Hibernate.initialize(p.getDistrito().getProvincia().getDepartamento().getIddepartamento());
                Hibernate.initialize(p.getIdproveedor());                
                listaResultados.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }    
    
    public ArrayList<Tipoproducto> listarInsumo(String nombre){
        ArrayList<Tipoproducto> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Tipoproducto where tipo='MP' and estado='1' and upper(nombre) like '%"+nombre+"%' order by idtipoproducto").list();         
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Tipoproducto p = (Tipoproducto) iter.next();
                Hibernate.initialize(p.getDescripcion());
                Hibernate.initialize(p.getIdtipoproducto());
                Hibernate.initialize(p.getMaxPolitica());
                Hibernate.initialize(p.getMinPolitica());
                Hibernate.initialize(p.getNombre());
                Hibernate.initialize(p.getEstado());
                Hibernate.initialize(p.getTipo());
                
                listaResultados.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }
    
    
    public ArrayList<Tipoproducto> listarMateriaPrima()
    {
        ArrayList<Tipoproducto> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Tipoproducto where tipo='MP' and estado='1' order by idtipoproducto").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                Tipoproducto p = (Tipoproducto) iter.next();
                Hibernate.initialize(p.getDescripcion());
                Hibernate.initialize(p.getIdtipoproducto());
                Hibernate.initialize(p.getMaxPolitica());
                Hibernate.initialize(p.getMinPolitica());
                Hibernate.initialize(p.getNombre());
                Hibernate.initialize(p.getEstado());
                Hibernate.initialize(p.getTipo());
                
                listaResultados.add(p);
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return listaResultados;
        }         
    }
    
    
    public Moneda listarMoneda(){
        Moneda moneda = new Moneda();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            List lista = session.createQuery("from Moneda where idmoneda = 1  order by idmoneda").list();        
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {    
                moneda = (Moneda) iter.next();
                Hibernate.initialize(moneda.getIdmoneda());
                Hibernate.initialize(moneda.getNombre());
              
                
            }        
            tx.commit();
        } catch(HibernateException e) {
            throw new HibernateException(e);    
        } finally {
            session.close();
            return moneda;
        }
    }
 }

